iT邦幫忙

2024 iThome 鐵人賽

DAY 7
0

今天要來分享如何把數據寫進MySQL的資料庫~

先找到我們的table
https://ithelp.ithome.com.tw/upload/images/20240921/201694069bYtfMAo4f.png

要把資料寫進去table需要用到這個語法

insert into;

但是在寫入table之前,我們需要知道這個table有什麼欄位,還有每個欄位的屬性,於是就要用到

describe;

這個語法可以可以快速查看表格中的每一個欄位的名稱、資料類型、是否允許NULL、鍵值資訊等,用昨天創建的student_information示範一次,這是昨天創建用到的語法

create table student_information (number int primary key, name varchar(50), gender varchar(50), age int);

https://ithelp.ithome.com.tw/upload/images/20240921/20169406tFp1rCUpiY.png
可以看到,describe student_information; 把我們創建時所設定的一些屬性顯示出來了,接下來就可以開始寫入資料了(我隨便亂填的

insert into student_information (number, name, gender, age) values(20240921, '祭余申', '只因', 25);

也可以一次寫入多筆資料,如

insert into student_information (number, name, gender, age) values(1000, 'I miss Saturday', '沃爾瑪購物袋', 1145),(183, '我想放假', '拜託了', 881) ;

用","隔開之後能輸入另一筆資料

寫入之後使用這個語法來查看table內的資料

select * from student_information;

"米字號"代表全部的欄位,若是只想查看個別欄位也可以把"米字號"改成欄位名稱,如"number",或是"age, gender" 分別可以查看number欄,或是age、gender欄,也可以對查詢設定一些限制,在最後加上where age > 18,就不會出現一些不想看到的資料了:Dhttps://ithelp.ithome.com.tw/upload/images/20240921/201694065eOY4tlHxm.png
https://ithelp.ithome.com.tw/upload/images/20240921/201694067vTyCHgqae.png


上一篇
D6:連接前置準備
下一篇
D8:連接測試
系列文
資料庫與Java開發工具連接26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言